<!-- 我的设备 -->

<template>
  <div class="RepairList-Step2-MyDevice">
    <span v-if="repair.assetName" class="el-button--text ellips" @click="toOpen">
      {{ repair.assetName }}
    </span>

    <span>
      <BtnText v-if="repair.assetId" title="清除" @click="toClear" />
      <BtnPrimary v-else title="选择" @click="toOpen" />
    </span>

    <EasyModal v-model="easyVisible" :title="`选择${CORE.MAP.ASSET}`" medium @submit="toSubmit">
      <ChooseDeviceOne :obj="obj" bind="device" />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyDevice',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb('deal')],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { device: {} }, // 数据对象
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 工单详情
     */
    repair() {
      return this.one.repair
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      if (CORE.isEmpty(this.obj.device)) {
        CORE.MSG.warning(`请选择${CORE.MAP.ASSET}`)
        return
      }

      const { device } = this.obj

      Object.assign(this.repair, {
        assetId: device.id,
        assetName: device.assetName,
        deviceId: device.deviceId,
        deviceName: device.deviceName,
      })

      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const { repair } = this
      const device = {
        id: repair.assetId,
        assetName: repair.assetName,
        deviceId: repair.deviceId,
        deviceName: repair.deviceName,
      }
      this.obj = { device }
    },

    /**
     * 清除
     */
    toClear() {
      Object.assign(this.repair, {
        assetId: '',
        assetName: '',
        deviceId: '',
        deviceName: '',
      })
      this.obj = { device: {} }
    },
  },
}
</script>
